package ezonsport.android.ui.stepSystem.dataHistory;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;

import ezonsport.android.R;
import ezonsport.android.application.SystemConstant;
import ezonsport.android.core.stepSystem.StepData;
import ezonsport.android.data.DataHelper;

/**
 *历史数据显示界面
 *
 *
 * 
 */
public class DataHistoryActivity extends OrmLiteBaseActivity<DataHelper> implements OnScrollListener {
	//Constant
	private static final int PAGE_SIZE = 10;
	static final String TAG = "DataHistoryActivity";
	
	private ListView mListView;
	private StepAdapter mListAdapter;
	private List<Map<String,Object>> mListItems;
	private String[] date = {"2012-04-15","2012-04-16","2012-04-17","2012-04-18","2012-04-19"};
	private String[] miles = {"2.5","3.23","5.4","10","4.3"};
	private String[] steps = {"1200","3200","5000","10000","4500"};
	private String[] calories = {"200","320","450","800","400"};
	
	private Calendar mUpCalendar;
	private Calendar mDownCalendar;
	//Controll and Model
	private DataHistoryController mDataHistoryCtr;
	private DataHistoryViewModel mDataHistoryModel;
	private List<StepData> mStepDataList;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.datahistory);
        
        mUpCalendar = Calendar.getInstance();
        mDownCalendar = (Calendar)mUpCalendar.clone();
        mDownCalendar.add(Calendar.DATE, -PAGE_SIZE+1);
        mDataHistoryCtr = new DataHistoryController(getHelper());
        mDataHistoryModel = mDataHistoryCtr.getDataHistory(mDownCalendar.getTime(), mUpCalendar.getTime());
        mStepDataList = mDataHistoryModel.getHistoryData();
        
        mListView = (ListView)findViewById(R.id.datahistory_datalist);
        mListItems = getListItems();
        mListAdapter = new StepAdapter(this, mListView, mListItems);
        mListView.setAdapter(mListAdapter);
        
        
	}
	
	/*public List<Map<String, Object>> getListItems(){
		List<Map<String, Object>> listItems = new ArrayList<Map<String,Object>>();
		
		for(int i=0; i<miles.length; i++){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put(SystemConstant.DataHistory.LISTITEM_DATE, date[i]);
			map.put(SystemConstant.DataHistory.LISTITEM_MILES, miles[i]);
			map.put(SystemConstant.DataHistory.LISTITEM_STEPS, steps[i]);
			map.put(SystemConstant.DataHistory.LISTITEM_CALORIES, calories[i]);
			listItems.add(map);
		}
		return listItems;
	}*/
	
	public List<Map<String, Object>> getListItems(){
		List<Map<String, Object>> listItems = new ArrayList<Map<String,Object>>();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		
		for(int i=0; i<mStepDataList.size(); i++){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put(SystemConstant.DataHistory.LISTITEM_DATE, 
					format.format(mStepDataList.get(i).getCollectTime()));
			map.put(SystemConstant.DataHistory.LISTITEM_MILES, 
					String.valueOf(mStepDataList.get(i).getDistance()));
			map.put(SystemConstant.DataHistory.LISTITEM_STEPS, 
					String.valueOf(mStepDataList.get(i).getSteps()));
			map.put(SystemConstant.DataHistory.LISTITEM_CALORIES, 
					String.valueOf(mStepDataList.get(i).getCalories()));
			listItems.add(map);
		}
		return listItems;
	}
	//获取PAGE_SIZE条数据
	public void getDataFromDB(){
		if(mDownCalendar!=null&&mUpCalendar!=null){
			mUpCalendar.add(Calendar.DATE, -PAGE_SIZE);
			mDownCalendar.add(Calendar.DATE, -PAGE_SIZE);
			mDataHistoryModel = mDataHistoryCtr.getDataHistory(mDownCalendar.getTime(), mUpCalendar.getTime());
	        mStepDataList = mDataHistoryModel.getHistoryData();
		}	
	}
	
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount){
		
	}
	
	public void onScrollStateChanged (AbsListView view, int scrollState){
		
	}
	
	public class StepAdapter extends BaseAdapter {
		private Context mContext;
		private List<Map<String, Object>> mListItems;
		private LayoutInflater mListContainer;
		private ListView mListView;
		
		boolean footerLoading = false;
		
		ViewSwitcher footerView;
		
		public final class ListItemView{
			public TextView mTextDate;
			public ImageView mImageShare;
			public TextView mTextMile;
			public TextView mTextStep;
			public TextView mTextCalory;
		}
		public StepAdapter(Context context, ListView listView, List<Map<String, Object>> listItems){
			mContext = context;
			mListContainer = LayoutInflater.from(context);
			mListView = listView;
			mListItems = listItems;
			footerView = (ViewSwitcher)mListContainer.inflate(R.layout.footer, null);
			TextView moreByFooter = (TextView)footerView.findViewById(R.id.datahistory_tx_loadmore);
			moreByFooter.setOnClickListener(new OnClickListener(){
				
				@Override
				public void onClick(View v){
					if(!footerLoading)
						new FooterTask().execute();
				}
			});
			mListView.addFooterView(footerView);
			
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mListItems.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ListItemView listItemView = null;
			if(convertView == null){
				listItemView = new ListItemView();
				convertView = mListContainer.inflate(R.layout.list_item, null);
				listItemView.mTextDate = (TextView)convertView.findViewById(R.id.listitem_tx_date);
				listItemView.mImageShare = (ImageView)convertView.findViewById(R.id.listitem_image_share);
				listItemView.mTextMile = (TextView)convertView.findViewById(R.id.listitem_tx_miles);
				listItemView.mTextStep = (TextView)convertView.findViewById(R.id.listitem_tx_steps);
				listItemView.mTextCalory = (TextView)convertView.findViewById(R.id.listitem_tx_calories);
				convertView.setTag(listItemView);
			}else{
				listItemView = (ListItemView)convertView.getTag();
			}
			listItemView.mTextDate.setText((String)mListItems.get(position).get(SystemConstant.DataHistory.LISTITEM_DATE));
			listItemView.mTextMile.setText((String)mListItems.get(position).get(SystemConstant.DataHistory.LISTITEM_MILES));
			listItemView.mTextStep.setText((String)mListItems.get(position).get(SystemConstant.DataHistory.LISTITEM_STEPS));
			listItemView.mTextCalory.setText((String)mListItems.get(position).get(SystemConstant.DataHistory.LISTITEM_CALORIES));
			return convertView;
		}
		class FooterTask extends AsyncTask<Void, Integer, List<StepData>>{
			
			@Override
			protected void onPreExecute() {
				footerLoading(true);
			}

			@Override
			protected  List<StepData> doInBackground(Void... params){
				getDataFromDB();
				return mStepDataList;
			}
			@Override
			protected void onPostExecute (List<StepData> stepList){
				if(stepList!=null){
					SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
					Log.v(TAG, String.valueOf(stepList.size()));
					for(int i=0; i<stepList.size(); i++){
						Map<String, Object> map = new HashMap<String, Object>();
						map.put(SystemConstant.DataHistory.LISTITEM_DATE, 
								format.format(stepList.get(i).getCollectTime()));
						map.put(SystemConstant.DataHistory.LISTITEM_MILES, 
								String.valueOf(stepList.get(i).getDistance()));
						map.put(SystemConstant.DataHistory.LISTITEM_STEPS, 
								String.valueOf(stepList.get(i).getSteps()));
						map.put(SystemConstant.DataHistory.LISTITEM_CALORIES, 
								String.valueOf(stepList.get(i).getCalories()));
						mListItems.add(map);
					}
				}
				notifyDataSetChanged();
				footerLoading(false);
			}
			
		}
		private void footerLoading(boolean isLoading) {
			if (isLoading) {
				footerView.setDisplayedChild(1);
			} else {
				footerView.setDisplayedChild(0);
			}
			footerLoading = isLoading;
		}
		
	}

	
	
}
